package com.world.rickcloudendpoint.models.result;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

/**
 * 分页返回数据结果
 *
 * @param <T>
 */
public class PageResult<T> implements Serializable {

    private static final long serialVersionUID = -6075483677503287942L;

    /**
     * 当前页
     */
    @JsonProperty("pageNum")
    private Integer pageNum;
    /**
     * 每页的数量
     */
    @JsonProperty("pageSize")
    private Integer pageSize;

    @JsonProperty("nextOffset")
    private Long nextOffset = null;

    @JsonProperty("priorOffset")
    private Long priorOffset = null;

    @JsonProperty("list")
    private List<T> data = null;

    @JsonProperty("total")
    private Long total = null;

    public PageResult() {
    }

    public PageResult(Integer pageNum, Integer pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public PageResult<T> nextOffset(Long nextOffset) {
        this.nextOffset = nextOffset;
        return this;
    }

    /**
     * 下一次偏移量
     *
     * @return nextPage
     **/
    @ApiModelProperty(value = "下一次偏移量")
    public Long getNextOffset() {
        return nextOffset;
    }

    public void setNextOffset(Long nextOffset) {
        this.nextOffset = nextOffset;
    }

    public PageResult<T> priorOffset(Long priorOffset) {
        this.priorOffset = priorOffset;
        return this;
    }

    /**
     * 上一次偏移量
     *
     * @return priorPage
     **/
    @ApiModelProperty(value = "上一次偏移量")
    public Long getPriorOffset() {
        return priorOffset;
    }

    public void setPriorOffset(Long priorOffset) {
        this.priorOffset = priorOffset;
    }

    @ApiModelProperty(value = "目标集合总数")
    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
        // 设置前后页码
        this.priorOffset = (null == pageNum || pageNum < 2) ? 1L : (pageNum - 1);
        if (null != pageNum && null != pageSize && pageSize > 0) {
            this.nextOffset = Long.valueOf(pageNum < (int) Math.ceil((double) total / pageSize) ? pageNum + 1
                    : (int) Math.ceil((double) total / pageSize));
        } else {
            this.nextOffset = 1L;
        }
    }

    public PageResult<T> list(List<T> data) {
        this.data = data;
        return this;
    }

    /**
     * Get list
     *
     * @return list
     **/
    @ApiModelProperty(value = "数据集")
    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    /**
     * 分页处理
     *
     * @param offset 起始位置
     * @param size   偏移量
     * @param list   对象列表
     */
    public void getPageResult(Long offset, Long size, LinkedList<T> list) {
        this.setTotal((long) list.size());
        this.setPriorOffset(offset);
        size = (size == -1) ? Long.MAX_VALUE : size;
        long nextOffset = (list.size() - offset) > size ? offset + size : list.size();
        this.setNextOffset(nextOffset);
        LinkedList<T> returnList = new LinkedList<>();
        for (long i = offset; i < offset + size && i < list.size(); i++) {
            returnList.addLast(list.get((int) i));
        }
        this.setData(returnList);
    }

    /**
     * 分页处理
     *
     * @param offset 起始位置
     * @param size   偏移量
     * @param list   对象列表
     */
    public void getPageResult(Long offset, Long size, Long total, LinkedList<T> list) {
        this.setTotal(total);
        this.setPriorOffset(offset);
        size = (size == -1) ? Long.MAX_VALUE : size;
        long nextOffset = (total - offset) > size ? offset + size : total;
        this.setNextOffset(nextOffset);
        this.setData(list);
    }
}
